package com.klun.concDesign.chaptersix;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class AskThread implements Runnable {

    CompletableFuture<Integer> re = null;

    public AskThread(CompletableFuture<Integer> re) {
        this.re = re;
    }

    @Override
    public void run() {
        int myRe = 0;
        try{
            myRe = re.get() * re.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(myRe);
    }

//    public static void main(String[] args) throws InterruptedException {
//        final CompletableFuture<Integer> future = new CompletableFuture<>();
//        new Thread(new AskThread(future)).start();
//        Thread.sleep(6000);
//        // 告知完成结果
//        future.complete(60);
//    }
    public static Integer calc(Integer para){
        try{
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return para*para;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        final CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> calc(50));
//        System.out.println(future.get());
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> calc(50))
                .thenApply((i) -> Integer.toString(i))
                .thenApply((str) -> "\"" + str + "\"")
                .thenAccept(System.out::println);
        future.get();
    }

    public static Integer calc1(Integer para){
        return para/0;
    }



}
